/**
 * @file factory_mode_reset.c
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include "lv_watch.h"

#if USE_LV_WATCH_FACTORY_MODE!= 0

#include <stdio.h>

/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/


/**********************
 *  STATIC PROTOTYPES
 **********************/
static void factorymode_reset_prepare_destory(lv_obj_t * activity_obj);
static void factoryreset_yes_btn_event_cb(lv_obj_t * btn, lv_event_t e);
static void factoryreset_no_btn_event_cb(lv_obj_t * btn, lv_event_t e);
/**********************
 *  STATIC VARIABLES
 **********************/

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
***********************/
lv_obj_t * factorymode_reset_create(lv_obj_t * activity_obj)
{
    lv_obj_t * obj;
    if(activity_obj == NULL) {
        lv_watch_activity_ext_t activity_ext;
        memset(&activity_ext, 0, sizeof(lv_watch_activity_ext_t));
        activity_ext.actId = ACT_ID_FACTORY_MODE_RESET;
        activity_ext.create = factorymode_reset_create;
        activity_ext.prepare_destory = factorymode_reset_prepare_destory;
        activity_obj = lv_watch_creat_activity_obj(&activity_ext);
        LV_ASSERT_MEM(activity_obj);
        if(NULL == activity_obj) {
            return NULL;
        }
    }

    obj = lv_watch_obj_create(activity_obj);
    LV_ASSERT_MEM(obj);
    if(NULL == obj) {
        return NULL;
    }

    lv_obj_t * label = lv_label_create(obj, NULL);
    lv_obj_set_size(label, LV_HOR_RES, LV_VER_RES / 6);
    lv_label_set_text_id(label, WATCH_TEXT_ID_FACTORY_RESET);
    lv_obj_add_style(label, LV_LABEL_PART_MAIN, &lv_watch_font20_gray);
    lv_obj_align(label, NULL, LV_ALIGN_IN_TOP_MID, 0, 25);

    lv_obj_t * label_tip = lv_label_create(obj, NULL);
    lv_label_set_text_id(label_tip, WATCH_TEXT_ID_FACTORY_RESET_Q);
    lv_label_set_long_mode(label_tip, LV_LABEL_LONG_BREAK);
    lv_obj_set_width(label_tip, LV_HOR_RES * 3 / 4);
    lv_obj_align(label_tip, label, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);
    lv_obj_add_style(label_tip, LV_LABEL_PART_MAIN, &lv_watch_font20);

    lv_obj_t * btn_no = lv_imgbtn_create(obj, NULL);
    lv_imgbtn_set_src(btn_no, LV_BTN_STATE_RELEASED, ICON_CANCEL);
    lv_imgbtn_set_src(btn_no, LV_BTN_STATE_PRESSED, ICON_CANCEL);
    lv_obj_align(btn_no, NULL, LV_ALIGN_IN_BOTTOM_LEFT, 30, -20);
    lv_obj_set_event_cb(btn_no, factoryreset_no_btn_event_cb);
    lv_watch_obj_add_element(btn_no);

    lv_obj_t * btn_yes = lv_imgbtn_create(obj, NULL);
    lv_imgbtn_set_src(btn_yes, LV_BTN_STATE_RELEASED, ICON_OK);
    lv_imgbtn_set_src(btn_yes, LV_BTN_STATE_PRESSED, ICON_OK);
    lv_obj_align(btn_yes, NULL, LV_ALIGN_IN_BOTTOM_RIGHT, -30, -20);
    lv_obj_set_event_cb(btn_yes, factoryreset_yes_btn_event_cb);
    lv_watch_obj_add_element(btn_yes);

    return obj;
}

/**********************
 *   STATIC FUNCTIONS
 **********************/
static void factorymode_reset_prepare_destory(lv_obj_t * activity_obj)
{
    lv_watch_png_cache_all_free();
}

static void factory_reset_handle(lv_task_t * task)
{
#if USE_LV_FLASH_FS
    Hal_Rtc_Settime(NULL);

    printf("Hal_Nvm_Factory_reset start to be called\n");
#endif
#if USE_LV_BLUETOOTH != 0
    bt_power_off();
#endif
#if USE_LV_WLAN != 0
    hal_wlan_reset();
#endif
    Hal_Nvm_Factory_reset();
}

static void factoryreset_yes_btn_event_cb(lv_obj_t * btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED == e)
    {
        watch_set_ready_state(false);
        lv_obj_t * par = lv_obj_get_parent(btn);
        lv_obj_clean(par);
        lv_obj_t * label = lv_label_create(par, NULL);
        lv_obj_add_style(label, LV_LABEL_PART_MAIN, &lv_watch_font20);
        lv_label_set_text_id(label, WATCH_TEXT_ID_FACTORY_RESETING);
        lv_label_set_long_mode(label, LV_LABEL_LONG_BREAK);
        lv_obj_set_width(label, LV_HOR_RES * 2 / 3);
        lv_obj_align(label, par, LV_ALIGN_CENTER, 0, 0);

        // delay reset for 50ms to ensure text displaying
        lv_task_t *task_p = lv_task_create(factory_reset_handle, 50, LV_TASK_PRIO_HIGHEST, NULL);
        lv_task_once(task_p);
    }
}

static void factoryreset_no_btn_event_cb(lv_obj_t * btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED == e)
    {
        lv_obj_t * act_obj = lv_watch_get_activity_obj(ACT_ID_FACTORY_MODE_RESET);
        lv_obj_del(act_obj);
    }
}

#endif /*USE_LV_WATCH_FACTORY_MODE*/
